<?php
namespace App\Components;
use Cache;
use Carbon\Carbon;
use Log;
/**
 * 抖音视频管理工具类
 */
class DyVideoTools {
  /**
   * 获取用户的视频列表
   */
  public static function searchVideoList($assess_token,$open_id,$cursor = 0,$count = 10){
    $url = "https://open.douyin.com/video/list/?access_token={$assess_token}&open_id={$open_id}&cursor={$cursor}&count={$count}";
    $res = DyTools::getJson($url);
    $data = isset($res['data']) ? $res['data'] : [];
    return $data;
  }
  
  /**
   * 上传图片
   */
  public static function imageUpload($assess_token,$open_id,$image,$name){
    $url = "https://open.douyin.com/image/upload/?access_token={$assess_token}&open_id={$open_id}";
    $payload = '';
    $params  = "--ABC1234\r\n"
      . "Content-Type: application/x-www-form-urlencoded\r\n"
      . "\r\n"
      . $payload . "\r\n"
      . "--ABC1234\r\n"
      . "Content-Type: image/jpeg\r\n"
      . "Content-Disposition: form-data; name=\"image\"; filename=\"".$name."\"\r\n"
      . "\r\n"
      . file_get_contents($image) . "\r\n"
      . "--ABC1234--";
    $first_newline      = strpos($params, "\r\n");
    $multipart_boundary = substr($params, 2, $first_newline - 2);
    $request_headers    = array();
    $request_headers[]  = 'Content-Length: '.strlen($params);
    $request_headers[]  = 'Content-Type: multipart/form-data; boundary='.$multipart_boundary;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $reply = curl_exec($ch);
    Log::info('imageUpload='.$reply);
    return json_decode($reply,true);
  }
  
  /**
   * 上传视频
   */
  public static function videoUpload($assess_token,$open_id,$video,$name,$cover_image=''){
    $url = "https://open.douyin.com/video/upload/?access_token={$assess_token}&open_id={$open_id}";
    $payload = '';
    $params  = "--ABC1234\r\n"
      . "Content-Type: application/x-www-form-urlencoded\r\n"
      . "\r\n"
      . $payload . "\r\n"
      . "--ABC1234\r\n"
      . "Content-Type: video/mp4\r\n"
      . "Content-Disposition: form-data; name=\"video\"; filename=\"".$name."\";custom_cover_image_url=\"".$cover_image."\"\r\n"
      . "\r\n"
      . file_get_contents($video) . "\r\n"
      . "--ABC1234--";
    $first_newline      = strpos($params, "\r\n");
    $multipart_boundary = substr($params, 2, $first_newline - 2);
    $request_headers    = array();
    $request_headers[]  = 'Content-Length: '.strlen($params);
    $request_headers[]  = 'Content-Type: multipart/form-data; boundary='.$multipart_boundary;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $reply = curl_exec($ch);
    Log::info('uploadFile='.$reply);
    return json_decode($reply,true);
  }

  /**
   * 发布视频
   */
  public static function videoCreate($assess_token,$open_id,$video_id,$video_title){
    $url = "https://open.douyin.com/video/create/?access_token={$assess_token}&open_id={$open_id}";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    $data = [
      'video_id'=>$video_id,
      'text'=>$video_title
    ];
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen(json_encode($data)))
    );
    $reply = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec返回TRUE 设置  curl_exec返回json(此处) 失败都返回FALSE
    curl_close($ch);
    Log::info('uploadFile='.$reply);
    return json_decode($reply,true);
  }
  
  /**
   * 视频分片初始化上传
   */
  public static function videoPartInit($assess_token,$open_id){
    $key = 'dy_video_part_init_'.$open_id;
    $result = Cache::get($key);
    if(!empty($result)){
      $result = unserialize($result);
    }else{
      $url = "https://open.douyin.com/video/part/init/?access_token={$assess_token}&open_id={$open_id}";
      $ch = curl_init($url);
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json')
      );
      $reply = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置  curl_exec返回TRUE 设置  curl_exec返回json(此处) 失败都返回FALSE
      curl_close($ch);
      Log::info('videoPartInit='.$reply);
      $reply = json_decode($reply,true);
      $result = (isset($reply['data']['upload_id']) ? $reply['data']['upload_id'] : FALSE);
      $expiresAt = Carbon::now()->addHours(1)->addMinutes(50);//缓存1小时50分钟    6600s
      Cache::put($key,serialize($result),$expiresAt);
    }
    Log::info("upload_id=".$result);
    return $result;
  }
  
  /**
   * 分片上传视频
   */
  public static function videoPartUpload($assess_token,$open_id,$video,$part_number = 1){
    $upload_id = self::videoPartInit($assess_token,$open_id);
    $upload_id = urlencode($upload_id);
    $url = "https://open.douyin.com/video/part/upload/?access_token={$assess_token}&open_id={$open_id}&upload_id={$upload_id}&part_number={$part_number}";
    $payload = '';
    $params  = "--ABC1234\r\n"
      . "Content-Type: application/x-www-form-urlencoded\r\n"
      . "\r\n"
      . $payload . "\r\n"
      . "--ABC1234\r\n"
      . "Content-Type: video/mp4\r\n"
      . "Content-Disposition: form-data; name=\"video\"; filename=\"".$part_number.".mp4\"\r\n"
      . "\r\n"
      . file_get_contents($video) . "\r\n"
      . "--ABC1234--";
    $first_newline      = strpos($params, "\r\n");
    $multipart_boundary = substr($params, 2, $first_newline - 2);
    $request_headers    = array();
    $request_headers[]  = 'Content-Length: '.strlen($params);
    $request_headers[]  = 'Content-Type: multipart/form-data; boundary='.$multipart_boundary;

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $reply = curl_exec($ch);
    Log::info('videoPartUpload='.$reply);
    return json_decode($reply,true);
  }
  
  /**
   * 分片完成上传
   */
  public static function videoPartComplete($assess_token,$open_id){
    $upload_id = self::videoPartInit($assess_token,$open_id);
    $upload_id = urlencode($upload_id);
    $url = "https://open.douyin.com/video/part/complete/?access_token={$assess_token}&open_id={$open_id}&upload_id={$upload_id}";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json')
    );
    $reply = curl_exec($ch);
    curl_close($ch);
    Log::info('videoPartComplete='.$reply);
    return json_decode($reply,true);
  }
}
